package mx.com.mindbits.bandpay.utils;

import mx.com.mindbits.bandpay.R;
import mx.com.mindbits.bandpay.asynctask.OpenDeviceTask;
import mx.com.mindbits.emvcardreader.usbinterface.CardCommands;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.util.Log;

public class USBReceiver extends BroadcastReceiver{	
	private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
	
	@Override
	public void onReceive(Context context, Intent intent) {		
		String action = intent.getAction();
		Intent broadcastIntent = new Intent();
		broadcastIntent.setAction(ConstantsValues.ACTION_USED);
		try{										
			if(UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)){
				UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
				ConstantsValues.dispositivo = device;
												
				broadcastIntent.putExtra(ConstantsValues.ACTION_ATTACHED, 
						context.getString(R.string.label_statusbar_attached));
				context.sendBroadcast(broadcastIntent);
				
				ConstantsValues.DEVICE_ALREADY_ATTACHED = true;
				Log.d(ConstantsValues.TAG_READER, "***************USB CONECTADO*******");					
			}			
			if(ACTION_USB_PERMISSION.equals(action)){
				
				synchronized(this){
					UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
					if(intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)){
						if(device != null){
							//the stuff	to open device						
							UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);																	
							ConstantsValues.tarjeta = new CardCommands(manager, device);							
							ConstantsValues.tarjeta = new OpenDeviceTask(ConstantsValues.tarjeta,context).execute().get();																																										
						}
						broadcastIntent.putExtra(ConstantsValues.ACTION_ATTACHED, context.getString(R.string.label_statusbar_opened));
						context.sendBroadcast(broadcastIntent);
						ConstantsValues.REQUEST_SENDED = true;
						Log.d(ConstantsValues.TAG_READER, "Accesorio USB CON PERMISOS");
					}else{
						Log.d(ConstantsValues.TAG_READER, "PERMISO DENEGADO.");						
					}
				}
			}
			
			if(UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)){
				ConstantsValues.tarjeta 		= null;
				ConstantsValues.dispositivo 	= null;
				ConstantsValues.REQUEST_SENDED  = false;
				ConstantsValues.DEVICE_ALREADY_ATTACHED = false;								
				broadcastIntent.putExtra(ConstantsValues.ACTION_ATTACHED, context.getString(R.string.label_statusbar_detached));
				context.sendBroadcast(broadcastIntent);
				
				Log.d(ConstantsValues.TAG_READER, "*************USB DESCONECTADO*************");				
			}
			
		}catch(Exception e){
			Log.e(ConstantsValues.TAG_READER, "ERROR EN EL BROADCAST RECEIVER: "+e);
		}		
	}//onReceive			
}//class
